﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">IoC</title>
  <keywords>
    <keyword translate="true">Inversion of Control</keyword>
    <keyword translate="true">IoC</keyword>
    <keyword translate="true">MEF</keyword>
    <keyword translate="true">Ninject</keyword>
    <keyword translate="true">ServiceLocator</keyword>
    <keyword translate="true">Unity</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">IoC</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Before Catel 2.0, the IoC container used internally was </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://unity.codeplex.com/" target="_blank" styleclass="Normal" translate="true">Unity</link><text styleclass="Normal" translate="true">. However, this forced all users to use and configure Unity as the IoC container in their apps and required the shipping of the libraries as well. Since Catel 2.0, a different technique is used which allows the end-developer to use the IoC container technique of their choice.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">ServiceLocator</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel uses it&apos;s own </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> implementing the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_IServiceLocator" styleclass="Normal" translate="true">IServiceLocator</link><text styleclass="Normal" translate="true"> to gather all services required by Catel. For example, default services are the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Services_IPleaseWaitService" styleclass="Normal" translate="true">IPleaseWaitService</link><text styleclass="Normal" translate="true"> and the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Services_IUIVisualizerService" styleclass="Normal" translate="true">IUIVisualizerService</link><text styleclass="Normal" translate="true">. By default, when the first view model is instantiated, Catel registers all default out of the box services to the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true">. However, it only does this when the specific services are not already registered. This allows an end-developer to register his/her own implementations of the services before any view model is instantiated (for example, at application startup). </text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> can be instantiated, but Catel instantiates one instance that can be used and shared amongst all objects inside the same AppDomain. The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> can be retrieved by using </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true">.Instance. The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ViewModelBase" styleclass="Normal" translate="true">ViewModelBase</link><text styleclass="Normal" translate="true"> implements </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_MVVM_ViewModelBase_GetService``1" styleclass="Normal" translate="true">GetService</link><text styleclass="Normal" translate="true"> which internally calls </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_IoC_IServiceLocator_ResolveType``1" styleclass="Normal" translate="true">ServiceLocator.ResolveType</link><text styleclass="Normal" translate="true">. This way, the end-developer does not have to think about instantiating or retrieving the right </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true">, but can simply use </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_MVVM_ViewModelBase_GetService``1" styleclass="Normal" translate="true">GetService</link><text styleclass="Normal" translate="true"> to retrieve the implementation of a specific registered interface.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering a type</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Registering a type in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> is very simple and works like any other IoC container:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Default.RegisterType&lt;IPleaseWaitService,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PleaseWaitService&gt;();</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering an instance of a type</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel uses </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx" target="_blank" styleclass="Normal" translate="true">Activator.CreateInstance</link><text styleclass="Normal" translate="true"> to create the interface implementations when the object is first resolved. However, sometimes a service constructor requires parameters or takes a long time to construct. In such cases, it is recommended to create the type manually and register the instance of the type:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">pleaseWaitService</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PleaseWaitService();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Default.RegisterInstance&lt;IPleaseWaitService&gt;(pleaseWaitService);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering a type via </text><text styleclass="Heading1" style="font-style:italic;" translate="true">MissingType</text><text styleclass="Heading1" translate="true"> event</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> gives the end-developer a last-resort chance to register a type when it is not registered in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> or any of the external containers. This event is very useful for logging (then the developer in the log knows exactly what type is missing from the IoC container) or it can be used to determine at runtime in a very late stage what implementation of the service must be used. To register a type via the event, subscribe to the event and then use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> OnMissingType(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">object</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> sender, MissingTypeEventArgs e)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">if</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> (e.InterfaceType == </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">typeof</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">(IPleaseWaitService))</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160; &#160;</text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">// Register an instance</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160; &#160;e.ImplementingInstance = </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> PleaseWaitService();</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160; &#160;</text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">// Or a type</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160; &#160;e.ImplementingType = </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">typeof</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">(PleaseWaitService);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;}</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">If both the </text><text styleclass="Normal" style="font-style:italic;" translate="true">ImplementingInstance</text><text styleclass="Normal" translate="true"> and </text><text styleclass="Normal" style="font-style:italic;" translate="true">ImplementingType </text><text styleclass="Normal" translate="true">are filled, the </text><text styleclass="Normal" style="font-style:italic;" translate="true">ImplementingIntance </text><text styleclass="Normal" translate="true">will be used.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Resolving a type</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To retrieve the implementation of a service, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">pleaseWaitService</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Default.ResolveType&lt;IPleaseWaitService&gt;();</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering external containers</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">As explained earlier, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> supports external containers to make sure that the end-developer can use the IoC container of his/her choice and is not forced by Catel to use any specific IoC implementation. Registering a container is very simple and can be done at any time (but, must of course be done before service registered in the external container can be used by the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> itself):</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Default.RegisterExternalContainer(myUnityContainer);</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Registering an external container makes it possible to resolve types from the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> that are originally registered in the external container. For example, a database repository is registered in Unity, but must be used inside a view model. This is what happens:</text></para>
    <list id="7" type="ol" listtype="decimal" formatstring="&#37;&#48;&#58;&#115;&#46;" format-charset="DEFAULT_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Arial; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">End-developer registers ICustomerRepository in a Unity container</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">End-developer registers the external container in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal (list)" translate="true">ServiceLocator</link><text styleclass="Normal (list)" translate="true"> of Catel</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">In a view model, the developer uses GetService&lt;ICustomerRepository&gt;() to retrieve the repository</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Internally, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal (list)" translate="true">ServiceLocator</link><text styleclass="Normal (list)" translate="true"> checks which external container has the type and uses that specific container to return the type</text></li>
    </list>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Synchronization between containers</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Until now, it all seems very nice. However, there are some aspects that are very hard and which must be taken into account. For example, think of the following issues:</text></para>
    <list id="8" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Instance caching</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Which container is the actual owner of a type?</text></li>
    </list>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Synchronization from external containers to ServiceLocator</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Internally, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> keeps a reference of all registered types in the internal container, all instantiated types and the original container per type. For example, think of the following situation:</text></para>
    <list id="9" type="ol" listtype="decimal" formatstring="&#37;&#48;&#58;&#115;&#46;" format-charset="DEFAULT_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Arial; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">A type is registered in Unity (but the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal (list)" translate="true">ServiceLocator</link><text styleclass="Normal (list)" translate="true"> does not know whether it&apos;s a type or instance since Unity makes no difference between them)</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">A developer resolves the type in the container</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> checks whether there is already an instance created or requested earlier by the developer. If it already has an instance in the cache, it simply returns the instance from the cache. If not, it checks which container (it might be itself, but also one of the external containers) is the owner of the type. A container is considered the owner if the type was first found on that specific container. The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> lets the external container (in this case Unity) instantiate the type. Then the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> stores the instance in the instance cache so it can be easily resolved in the next call.</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Synchronization from ServiceLocator to external containers</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Great, now think of this situation:</text></para>
    <list id="10" type="ol" listtype="decimal" formatstring="&#37;&#48;&#58;&#115;&#46;" format-charset="DEFAULT_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Arial; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">A type is registered in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal (list)" translate="true">ServiceLocator</link></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The type must be injected into a constructor using Unity</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">This is a bit harder because the type is not registered in Unity, but it is used by Unity. Luckily, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> is able to automatically register types in all external containers as soon as a type is registered or resolved. This way, when a type is registered in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true">, it automatically calls </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_IoC_IExternalContainerHelper_RegisterType_3_6b1c5e75" styleclass="Normal" translate="true">ExternalContainer.RegisterType</link><text styleclass="Normal" translate="true"> (or (</text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_IoC_IExternalContainerHelper_RegisterInstance_3_fac346bd" styleclass="Normal" translate="true">ExternalContainer.RegisterInstance</link><text styleclass="Normal" translate="true"> in case of an instance) for all external containers. This way, it is possible to register a type in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true">, but still be able to use the powerful dependency injection of Unity.</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Automatic or manual synchronization</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">By default, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> automatically keeps all the IoC containers in sync so the end-developer should not have any knowledge of the inner workings of the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true">. Sometimes, a developer wants to have more control and needs to disable this automatic behavior. This is possible using the following property:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.AutomaticallyKeepContainersSynchronized</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">false</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">;</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Then it is still possible to manually synchronize the containers using the following methods:</text></para>
    <list id="11" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_IoC_ServiceLocator_ExportInstancesToExternalContainers" styleclass="Normal (list)" translate="true">ExportInstancesToExternalContainers</link><text styleclass="Normal (list)" translate="true"> =&gt; exports instances only</text></li>
      <li styleclass="Normal (list)"><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_IoC_ServiceLocator_ExportToExternalContainers" styleclass="Normal (list)" translate="true">ExportToExternalContainers</link><text styleclass="Normal (list)" translate="true"> =&gt; exports instances and registered types</text></li>
    </list>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">External containers that only support instances</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">There are external containers, such as MEF, that only support the registration of instances, not types. If such an external container is registered, all the types registered in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> are instantiated and registered as an instance in the external container. If this behavior is not acceptable, disable the automatic synchronization and handle the synchronization manually via the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="P_Catel_IoC_ServiceLocator_AutomaticallyKeepContainersSynchronized" styleclass="Normal" translate="true">AutomaticallyKeepContainersSynchronized</link><text styleclass="Normal" translate="true"> property.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">External containers supported</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Currently, the following external IoC containers are supported:</text></para>
    <list id="12" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Castle Windsor</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">MEF</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Ninject</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Unity</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">If there is need for other containers, just let us know!</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Advanced information</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> is a pretty advanced implementation of IoC without the requirements of any reference to, for example, Unity. There are a few inner workings that are very important that are explained in this section. Internally, this is the workflow that the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal" translate="true">ServiceLocator</link><text styleclass="Normal" translate="true"> use to resolve types:</text></para>
    <list id="13" type="ol" listtype="decimal" formatstring="&#37;&#48;&#58;&#115;&#46;" format-charset="DEFAULT_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Arial; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Check if an instance is already created so it can be returned</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Check if the type is registered in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal (list)" translate="true">ServiceLocator</link><text styleclass="Normal (list)" translate="true"> itself</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Loop all external containers and check whether the type is registered there</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">If the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal (list)" translate="true">ServiceLocator</link><text styleclass="Normal (list)" translate="true"> nor the external containers have the type registered, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_IoC_ServiceLocator" styleclass="Normal (list)" translate="true">ServiceLocator</link><text styleclass="Normal (list)" translate="true"> raises the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="E_Catel_IoC_ServiceLocator_MissingType" styleclass="Normal (list)" translate="true">MissingType</link><text styleclass="Normal (list)" translate="true"> event which gives the developer a last opportunity to either log the missing type or register it via the event</text></li>
    </list>
  </body>
</topic>
